/**
 * 
 */
package com.manage.service;

import java.util.List;

import org.apache.log4j.Logger;

import com.auth.model.Account;
import com.manage.model.ThreePayParamater;
import com.manage.config.Config;
import com.service.AbstractService;
import com.util.ConfigUtil;
import com.utils.dbcp.ConnectionManager;
import com.utils.dbcp.ModelGenerator;

public class ThreePayParamaterServiceImpl extends AbstractService implements ThreePayParamaterService
{
	private Logger logger;
	
	public ThreePayParamaterServiceImpl()
	{
		logger = Logger.getLogger(this.getClass());
		this.dbcp = ConfigUtil.get(Config.SECTION, Config.KEY_DBCP_NAME);
	}
		
	@Override
	public ThreePayParamater getThreePayParamater(int id) 
	{
		String sql = "select * from threepayparamater where id=" + String.valueOf(id) + "";
		ModelGenerator<ThreePayParamater> dbcpmg = new ModelGenerator<ThreePayParamater>(ThreePayParamater.class);
		return dbcpmg.generate(dbcp, sql);
	}

	@Override
	public List<ThreePayParamater> getThreePayParamaters()
	{
		String sql = "select * from threepayparamater order by id";
		logger.debug("sql: " + sql);
		ModelGenerator<ThreePayParamater> dbcpmg = new ModelGenerator<ThreePayParamater>(ThreePayParamater.class);
		return dbcpmg.generateList(dbcp, sql);
	}
	
	@Override
	public boolean saveThreePayParamater(Account operator, String type, ThreePayParamater model)
	{
		if (operator == null || operator.getId() == null || operator.getId().length() == 0)
			return false;
		
		String sql = "";
		if (type.equalsIgnoreCase("insert"))
		{
			
			model.setId(getTableId("threepayparamater"));
			sql = 
				"insert into threepayparamater(id,threepayid,hashkey,hashvalue) " +
				"values(" + String.valueOf(model.getId()) + ",'"+model.getThreepayid()+"','" 
				+ encodeDb(model.getHashkey()) + "','" + encodeDb(model.getHashvalue()) + 
				"')";
		}
		else if (type.equalsIgnoreCase("update"))
		{
			sql = 
				"update threepayparamater set hashkey='" + encodeDb(model.getHashkey()) + "'," +
						"hashvalue='" + encodeDb(model.getHashvalue()) + "' " +
				" where id=" + String.valueOf(model.getId()) + "";
		}
		else if (type.equalsIgnoreCase("delete"))
		{
			sql = "delete from threepayparamater where id=" + String.valueOf(model.getId()) + "";
		}
		else
		{
			sql = "";
		}
		logger.debug("sql: " + sql);
		if (sql.length() > 0)
		{
			if (ConnectionManager.getInstance().execute(dbcp, sql))
			{
				ThreePayParamater lo = null;
				if (!type.equals("delete"))
				{
					lo = getThreePayParamater(model.getId());
				}
				logTableOperate("threepayparamater", type, operator, lo);
				return true;
			}
		}		
		return false;
	}
}
